import pyautogui
import win32gui
import numpy as np


def click_on_target(hwnd, class_id, center_x, center_y, dart_center_x, dart_center_y, window_width, window_height,
                    offset_x=10, offset_y=10):
    # 获取窗口坐标和尺寸
    left, top, _, _ = win32gui.GetWindowRect(hwnd)

    # 假设目标类别为 1
    target_class_id = 1
    if class_id == target_class_id:
        # 应用点击偏移量
        q = center_x + 1 + offset_x
        t = center_y + 1 + offset_y
        # 计算相对移动距离
        dx = q - dart_center_x
        dy = t - dart_center_y
        distance = np.sqrt(dx ** 2 + dy ** 2)
        while distance > 3:
            # 获取当前鼠标位置
            current_x, current_y = pyautogui.position()
            new_x = current_x + dx
            new_y = current_y + dy

            # 限制鼠标在窗口内移动
            new_x = max(left, min(new_x, left + window_width))
            new_y = max(top, min(new_y, top + window_height))

            dx = new_x - current_x
            dy = new_y - current_y
            pyautogui.moveRel(dx, dy)

            # 这里暂时不重新获取截图和检测飞镖位置，若需要可添加相应逻辑
            # 若要添加，可参考之前的代码获取截图并重新检测飞镖位置

            dx = q - dart_center_x
            dy = t - dart_center_y
            distance = np.sqrt(dx ** 2 + dy ** 2)

        pyautogui.click()
